package com.sgb.kjwl.viewmodel.transport;

import android.text.TextUtils;
import android.widget.Toast;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.R;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.UserMsgEntity;
import com.sgb.kjwl.model.entity.self.RunningCityEntity;
import com.sgb.kjwl.model.entity.self.RunningCityPEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.repository.TransportRepertory;
import com.sgb.kjwl.utils.AddressHelper;
import com.sgb.kjwl.view.ui.activity.transport.MainCitiesActivity;
import com.sgb.kjwl.view.ui.fragment.transport.CarsFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainCitiesModel extends BaseViewModel {

    private MainCitiesActivity mainCitiesActivity;
    private TransportRepertory repertory;
    public ObservableField<RunningCityPEntity> entity = new ObservableField<>(new RunningCityPEntity());

    public MainCitiesModel(MainCitiesActivity mainCitiesActivity, TransportRepertory repertory) {
        this.mainCitiesActivity = mainCitiesActivity;
        this.repertory = repertory;
    }


    public void getAddressModel() {
        AddressHelper.getInstance().getAllArea(new AddressHelper.AddressCallBack() {
            @Override
            public void initialData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> data) {
                if (data != null && data.size() > 0) {
                    mainCitiesActivity.showAddressPop(data);
                }
            }

            @Override
            public void threeData(List<AddressEntity> provinceList, List<List<AddressEntity>> cityList, List<List<List<AddressEntity>>> districtList) {

            }
        });
    }

    public void frequentlyUsedWay() {
        DialogHelper.showProgressDialog(mainCitiesActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().frequentlyUsedWay().enqueue(new Callback<BaseEntity<RunningCityPEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<RunningCityPEntity>> call, Response<BaseEntity<RunningCityPEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response == null || response.body() == null)
                    return;
                if (response.body().getData() != null && response.body().getState().equals("ok")) {
                    mainCitiesActivity.getSetWay(response.body().getData());
                } else {
                    Toast.makeText(mainCitiesActivity, response.message(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<RunningCityPEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void saveWay() {
        Map<String, Object> map = new HashMap<>();
        if (entity.get().getId() != null && entity.get().getId() != 0) {
            map.put("id", entity.get().getId());
        }
        map.put("freight", entity.get().getFreight());
        map.put("freightUnit", entity.get().getFreightUnit());
        map.put("list", entity.get().getSaveWayDTOList());

        DialogHelper.showProgressDialog(mainCitiesActivity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().saveWay(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response == null || response.body() == null)
                    return;
                if (response.body().getState().equals("ok")) {
                    Toast.makeText(mainCitiesActivity, "设置成功", Toast.LENGTH_SHORT).show();
                    mainCitiesActivity.finish();
                } else {
                    Toast.makeText(mainCitiesActivity, response.message(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getUserInfo() {
        DialogHelper.showProgressDialog(mainCitiesActivity, null, "请求中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getUserInfo().enqueue(new Callback<BaseEntity<UserMsgEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<UserMsgEntity>> call, Response<BaseEntity<UserMsgEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getData() == null) return;
                if (response.body().getState().equals("ok")) {
                    mainCitiesActivity.showAuth(response.body().getData());
                } else {
                    MToast.showToast(mainCitiesActivity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<UserMsgEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
